package com.misc.tunewsreader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SmileyParser {
    private HashMap<String, String> smileys = null;
    private List<String> smiley_txt = null;
    private Pattern regex = null;

    public void init() {
	smileys = new HashMap<String, String>();
	smiley_txt = new ArrayList<String>();
	smiley_txt.add(":-)");
	smiley_txt.add(";-)");
	smiley_txt.add(":-P");
	smiley_txt.add("=-O");
	smiley_txt.add(":-*");
	smiley_txt.add(":O");
	smiley_txt.add("B-)");
	smiley_txt.add(":-$");
	smiley_txt.add(":-!");
	smiley_txt.add(":-[");
	smiley_txt.add("O:-)");
	smiley_txt.add(":-\\");
	smiley_txt.add(":'(");
	smiley_txt.add(":-X");
	smiley_txt.add(":-D");
	smiley_txt.add(":-(");

	smiley_txt.add(":)");
	smiley_txt.add(";)");
	smiley_txt.add(":(");
	smiley_txt.add(":D");
	smiley_txt.add(":P");

	smileys.put(smiley_txt.get(0), "emo_im_happy.png");
	smileys.put(smiley_txt.get(1), "emo_im_winking.png");
	smileys.put(smiley_txt.get(2), "emo_im_tongue_sticking_out.png");
	smileys.put(smiley_txt.get(3), "emo_im_surprised.png");
	smileys.put(smiley_txt.get(4), "emo_im_kissing.png");
	smileys.put(smiley_txt.get(5), "emo_im_yelling.png");
	smileys.put(smiley_txt.get(6), "emo_im_cool.png");
	smileys.put(smiley_txt.get(7), "emo_im_money_mouth.png");
	smileys.put(smiley_txt.get(8), "emo_im_foot_in_mouth.png");
	smileys.put(smiley_txt.get(9), "emo_im_embarrassed.png");
	smileys.put(smiley_txt.get(10), "emo_im_angel.png");
	smileys.put(smiley_txt.get(11), "emo_im_undecided.png");
	smileys.put(smiley_txt.get(12), "emo_im_crying.png");
	smileys.put(smiley_txt.get(13), "emo_im_lips_are_sealed.png");
	smileys.put(smiley_txt.get(14), "emo_im_laughing.png");
	smileys.put(smiley_txt.get(15), "emo_im_sad.png");

	smileys.put(smiley_txt.get(16), "emo_im_happy.png");
	smileys.put(smiley_txt.get(17), "emo_im_winking.png");
	smileys.put(smiley_txt.get(18), "emo_im_sad.png");
	smileys.put(smiley_txt.get(19), "emo_im_laughing.png");
	smileys.put(smiley_txt.get(20), "emo_im_tongue_sticking_out.png");

	regex = buildRegex();
    }

    private Pattern buildRegex() {
	StringBuilder patternString = new StringBuilder();

	// build regex (escape smileys). smileys must have whitespace (or line
	// beginning/ending) before and after smiley
	patternString.append("(^|\\s)(");
	for (String s : smiley_txt) {
	    patternString.append(Pattern.quote(s));
	    patternString.append('|');
	}
	// replace last '|' with ')'
	patternString.replace(patternString.length() - 1,
		patternString.length(), ")");
	patternString.append("($|\\s|\\r|\\n)");

	return Pattern.compile(patternString.toString());
    }

    public String parse(String text) {
	StringBuilder ret = new StringBuilder(text);
	Matcher matcher = regex.matcher(ret);
	// replace smileys
	while (matcher.find()) {
	    String img = smileys.get(matcher.group(2));
	    ret = new StringBuilder(text.substring(0, matcher.start(2)));
	    ret.append("<img src=\"file:///android_asset/" + img
		    + "\" style=\"vertical-align: text-bottom;\"/>");
	    ret.append(text.substring(matcher.end(2)));
	    text = ret.toString();
	    matcher = regex.matcher(text);
	}
	return text;
    }

}
